<?php

namespace Cloud\Support;

class Algorithm
{
    /**
     * 轮询，最后连接的 index
     *
     * @var int
     */
    protected $lastUsingAddrIndex = 0;

    /**
     * Note: 查询下一个 IM Server
     *
     * @param array $servers
     * @return mixed
     * @author: Zhao Zhiqiang
     * @since: 2021/6/4 15:37
     */
    public function getNextAddr(array $servers)
    {
        $servers = array_values($servers);

        if ($this->lastUsingAddrIndex + 1 > count($servers) - 1) {
            return $servers[0];
        } else {
            return $servers[$this->lastUsingAddrIndex + 1];
        }
    }
}